﻿<%@ WebHandler Language="C#" Class="ashCompania" %>

using System;
using System.Configuration;
using System.Web;
using System.IO;
using System.Data;
using System.Data.SqlClient;

public class ashCompania : IHttpHandler 
{
    public void ProcessRequest(HttpContext context)
    {
        Int32 empno;
       if (context.Request.QueryString["id"] != null)
                empno = Convert.ToInt32(context.Request.QueryString["id"]);
            else
                throw new ArgumentException("No parameter specified");

            context.Response.ContentType = "image/jpeg";
            Stream strm = ShowEmpImage(empno);
            byte[] buffer = new byte[4096];
            int byteSeq = strm.Read(buffer, 0, 4096);

            while (byteSeq > 0)
            {
                context.Response.OutputStream.Write(buffer, 0, byteSeq);
                byteSeq = strm.Read(buffer, 0, 4096);
            }
            //context.Response.BinaryWrite(buffer);
    }

    public Stream ShowEmpImage(int empno)
    {
        WCCompania.BECompania be = new WCCompania.BECompania
        {
            IdCompania = empno
        };
        be = ObtenerCompania(be);
        try
        {
            return new MemoryStream(be.ImagenLogo);
        }
        catch 
        {
            return null;
        }
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
    /// <summary>
    /// Método Obtiene el Tipo de Puesto
    /// </summary>
    /// <returns>Devuelve un Objeto</returns>
    protected WCCompania.BECompania ObtenerCompania(WCCompania.BECompania pbe)
    {
        WCCompania.BECompania be = new WCCompania.BECompania();
        DataTable dt = new DataTable();
        using (var client = new ServiceClient<WCCompania.IWCCompania>("BasicHttpBinding_IWCCompania"))
        {
            var response = client.ServiceOperation<WCCompania.BECompania>(
               c => c.Obtener(pbe)
               );
            ;
            be = response;
        }
        return be;
    }
    
}
